package com.business.form.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.business.form.po.FormBean;
import com.business.frame.service.FieldService;
import com.common.ContextUtils;
import com.common.FileUtil;
import com.common.NumberProcess;
import com.common.StringProcess;

@Controller
public class FormPublish {

	private boolean isStandardCode=true;
	private String enterString="\r\n";
	private String indentString="\t";
	
	private static FormPublish newInstance=null;
	public static FormPublish NewInstance()
	{
		if(newInstance==null)
		{
			newInstance=new FormPublish();
		}
		return newInstance;
	}
	
	public String OnPublic(HttpServletRequest request,String formId,Map<String,Object> service,FieldService fieldService)
	{
		String msg="1";
		if(!isStandardCode){
			enterString="";
			indentString="";
		}
		
		FormService formService=(FormService)service.get("Form");
		
		FormBean formBean=formService.getBeanByKeyId(formId);
		if(formBean!=null)
		{
			String formData=formBean.getFormData();
			if(formData!=null && formData.length()>0)
			{
				Map<String,Object> formDataMap=JSON.parseObject(formData, new TypeReference<Map<String,Object>>(){});
				if(formDataMap.get("Form")==null)
				{
					return "FormData里找不到Form";
				}
				else if(formDataMap.get("FormButton")==null)
				{
					return "FormData里找不到FormButton";
				}
				String pageClassifyCode=formBean.getPageClassifyCode();
				if(pageClassifyCode.equals("bdy"))
				{
					if(formDataMap.get("FormSave")==null)
					{
						return "FormData里找不到FormSave";
					}
					else if(formDataMap.get("FormControl")==null)
					{
						return "FormData里找不到FormControl";
					}
				}
				else if(pageClassifyCode.equals("lby") || pageClassifyCode.equals("tjy"))
				{
					if(formDataMap.get("FormRead")==null)
					{
						return "FormData里找不到FormRead";
					}
					else if(formDataMap.get("FormColumn")==null)
					{
						return "FormData里找不到FormColumn";
					}
				}
				
				//验证完毕，开始生成
				
				List<Map<String,Object>> listFormButton=(List<Map<String,Object>>)formDataMap.get("FormButton");
				Object objFormTab=formDataMap.get("FormTab");
				List<Map<String,Object>> listFormTab=new ArrayList<Map<String, Object>>();
				if(objFormTab!=null)
				{
					listFormTab=(List<Map<String,Object>>)formDataMap.get("FormTab");
					List<Map<String,Object>> listTemp=new ArrayList<Map<String, Object>>();
					for(int i=0;i<listFormTab.size();i++)
					{
						if(StringProcess.convertToString(listFormTab.get(i).get("IsShow")).equals("1"))
						{
							listTemp.add(listFormTab.get(i));
						}
					}
					listFormTab=listTemp;
				}
				List<Map<String,Object>> listFormControl=(List<Map<String,Object>>)formDataMap.get("FormControl");
				List<Map<String,Object>> listFormColumn=(List<Map<String,Object>>)formDataMap.get("FormColumn");
				List<Map<String,Object>> listFormRead=(List<Map<String,Object>>)formDataMap.get("FormRead");
				List<Map<String,Object>> listFormSave=(List<Map<String,Object>>)formDataMap.get("FormSave");
				Object objFormControlParams=formDataMap.get("FormControlParams");
				List<Map<String,Object>> listFormControlParams=new ArrayList<Map<String, Object>>();
				if(objFormControlParams!=null)
				{
					listFormControlParams=(List<Map<String,Object>>)formDataMap.get("FormControlParams");
				}
				Object objFormColumnParams=formDataMap.get("FormColumnParams");
				List<Map<String,Object>> listFormColumnParams=new ArrayList<Map<String, Object>>();
				if(objFormColumnParams!=null)
				{
					listFormColumnParams=(List<Map<String,Object>>)formDataMap.get("FormColumnParams");
				}
				Object objFormFilter=formDataMap.get("FormFilter");
				List<Map<String,Object>> listFormFilter=new ArrayList<Map<String, Object>>();
				if(objFormFilter!=null)
				{
					listFormFilter=(List<Map<String,Object>>)formDataMap.get("FormFilter");
				}
				List<Object> listTableField=new ArrayList<Object>();
				if(listFormSave!=null)
				{
					for(int i=0;i<listFormSave.size();i++)
					{
						String tableId=StringProcess.convertToString(listFormSave.get(i).get("SaveTableId"));
						Map<String, Object> searchField = new HashMap<String, Object>();
						searchField.put("tableId", tableId);
						List<Map<String,Object>> listField=fieldService.select(searchField);
						
						listTableField.add(listField);
					}
				}
				
				
				StringBuilder sb = new StringBuilder();
				sb.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
				sb.append(enterString);
				sb.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
				sb.append(enterString);
				sb.append("<head>");
				sb.append(enterString);
				sb.append(indentString+"<title>"+formBean.getFormTitle()+"</title>");
				sb.append(enterString);

				//引用css文件
				sb.append(indentString+"<link href=\"/static/css/layout.css\" rel=\"stylesheet\" type=\"text/css\" />");
				sb.append(enterString);
				sb.append(indentString+"<link href=\"/static/css/layout.css\" rel=\"stylesheet\" type=\"text/css\" />");
				sb.append(enterString);
				sb.append(indentString+"<link href=\"/static/css/dialog.css\" rel=\"stylesheet\" type=\"text/css\" />");
				sb.append(enterString);
				if(pageClassifyCode.equals("bdy"))
				{
					sb.append(indentString+"<link href=\"/static/css/tab.css\" rel=\"stylesheet\" type=\"text/css\" />");
					sb.append(enterString);
				}
				sb.append(enterString);
				
				
				//引用Js文件
				sb.append(indentString+"<script src=\"/static/Js/jquery-1.4.2.js\" type=\"text/javascript\"></script>");
				sb.append(enterString);
				sb.append(indentString+"<script src=\"/static/js/layout.js\" type=\"text/javascript\"></script>");
				sb.append(enterString);
				sb.append(indentString+"<script src=\"/static/js/dialog.js\" type=\"text/javascript\"></script>");
				sb.append(enterString);
				if(pageClassifyCode.equals("bdy"))
				{
					sb.append(indentString+"<script src=\"/static/js/tab.js\" type=\"text/javascript\"></script>");
					sb.append(enterString);
					sb.append(indentString+"<script src=\"/static/js/Calendar5.js\" type=\"text/javascript\"></script>");
					sb.append(enterString);
				}
				sb.append(indentString+"<script src=\"/static/js/form.js\" type=\"text/javascript\"></script>");
				sb.append(enterString);
				
				
				//初始化Js代码
				sb.append(enterString);
				sb.append(indentString+"<script>");
				sb.append(enterString);
				sb.append(indentString+indentString+"var RootPath = \""+ContextUtils.getContextPath(request)+"\";");
				sb.append(enterString);
				sb.append(indentString+indentString+"var FormCodeUrl = RootPath + \"/base\";");
				sb.append(enterString);
				if(pageClassifyCode.equals("bdy"))
				{
					sb.append(indentString+indentString+"var FormSubmitMethod = \"save\";");
					sb.append(enterString);
					sb.append(indentString+indentString+"var FormSubmitAction = \"insert\";");
					sb.append(enterString);
					sb.append(enterString);
					sb.append(indentString+indentString+"var c = new Calendar(\"c\");");
					sb.append(enterString);
					sb.append(indentString+indentString+"document.write(c);");
					sb.append(enterString);
					sb.append(enterString);
					sb.append(indentString+indentString+"function SubmitClick() {");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"if (!CheckForm()) {");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"return false;");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"} else {");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"SubmitForm();");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"}");
					sb.append(enterString);
					sb.append(indentString+indentString+"}");
					sb.append(enterString);
					
					sb.append(enterString);
					sb.append(indentString+indentString+"function ClearValue(id)");
					sb.append(enterString);
					sb.append(indentString+indentString+"{");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"document.getElementById(id).value = \"\";");
					sb.append(enterString);
					sb.append(indentString+indentString+"}");
					sb.append(enterString);
					sb.append(enterString);
					
					sb.append(indentString+indentString+"$(function() {");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"var submitFormId=GetUrlValue(\"submitFormId\");");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"var keyId=GetUrlValue(\"keyId\");");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"if(submitFormId.length>0 && keyId.length>0)");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"{");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"InitFormControlValue(submitFormId,keyId);");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"FormSubmitAction = \"update\";");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"}");
					sb.append(enterString);
					sb.append(indentString+indentString+"});");
					sb.append(enterString);
					sb.append(enterString);
				}
				else
				{
					sb.append(indentString+indentString+"var FormQuery = \"/query\";");
					sb.append(enterString);
					sb.append(indentString+indentString+"var FormTableId = \"GridView1\";");
					sb.append(enterString);
					sb.append(indentString+indentString+"var TablePageSize = 10;");
					sb.append(enterString);
					sb.append(indentString+indentString+"var FormDeleteMethod = \"delete\";");
					sb.append(enterString);
					
					sb.append(indentString+indentString+"$(function() {");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"SearchPage(1);");
					sb.append(enterString);
					sb.append(indentString+indentString+"});");
					sb.append(enterString);
					sb.append(enterString);
					
					//button的script
					for(int i=0;i<listFormButton.size();i++)
					{
						Map<String,Object> itemFormButton=listFormButton.get(i);
						
						String script=StringProcess.convertToString(itemFormButton.get("ButtonScript"));
						if(script.length()>0)
						{
							script=StringProcess.decode(script);
							sb.append(script);
							sb.append(enterString);
							sb.append(enterString);
						}
					}
				}
				

				

				sb.append(indentString+"</script>");
				sb.append(enterString);
				
				
				
				sb.append("</head>");
				sb.append(enterString);
				sb.append("<body>");
				sb.append(enterString);
				
				
				sb.append(indentString+"<form id=\"myform\">");
				sb.append(enterString);
				if(pageClassifyCode.equals("bdy"))
				{
					sb.append(indentString+indentString+"<input type=\"hidden\" id=\"hidSubmitFormId\" name=\"hidSubmitFormId\" value=\""+formId+"\" />");
					sb.append(enterString);
				}
				else if(pageClassifyCode.equals("lby"))
				{
					sb.append(indentString+indentString+"<input type=\"hidden\" id=\"hidListFormId\" name=\"hidListFormId\" value=\""+formId+"\" />");
					sb.append(enterString);
				}
				sb.append(indentString+indentString+"<div class=\"container\">");
				sb.append(enterString);
				if(pageClassifyCode.equals("lby"))
				{
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullheight w4\"></div>");
					sb.append(enterString);
				}
				sb.append(indentString+indentString+indentString+"<div class=\"fullheight calcwidth\">");
				sb.append(enterString);
				sb.append(indentString+indentString+indentString+indentString+"<div class=\"fullwidth\">");
				sb.append(enterString);
				sb.append(indentString+indentString+indentString+indentString+indentString+"<div class=\"buttonGroup h30\">");
				sb.append(enterString);
				//按钮
				for(int i=0;i<listFormButton.size();i++)
				{
					Map<String,Object> itemFormButton=listFormButton.get(i);
					
					sb.append(indentString+indentString+indentString+indentString+indentString+indentString);
					sb.append("<input type=\"button\" value=\""+StringProcess.convertToString(itemFormButton.get("ButtonTitle"))+"\" id=\"btn_"+StringProcess.convertToString(itemFormButton.get("ButtonId"))+"\" onclick=\""+StringProcess.convertToString(itemFormButton.get("ButtonAction"))+"\" />");
					sb.append(enterString);
				}
				sb.append(indentString+indentString+indentString+indentString+indentString+"</div>");
				sb.append(enterString);
				sb.append(indentString+indentString+indentString+indentString+"</div>");
				sb.append(enterString);
				
				if(pageClassifyCode.equals("bdy"))
				{
					if(listFormTab.size()>1)
					{
						//有tab页
						sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+"<div class=\"tabGroup fullwidth\">");
						sb.append(enterString);
						for(int i=0;i<listFormTab.size();i++)
						{
							Map<String,Object> itemFormTab=listFormTab.get(i);
							
							String formTabId=StringProcess.convertToString(itemFormTab.get("FormTabId"));
							String formTabTitle=StringProcess.convertToString(itemFormTab.get("FormTabTitle"));
							String selected=" selected";
							if(i>0)
							{
								selected="";
							}
							
							sb.append(indentString+indentString+indentString+indentString+"<div class=\"tab fullheight"+selected+"\" id=\"tab"+formTabId+"\" target=\"page"+formTabId+"\">");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+"<span class=\"tableft\"></span><font>"+formTabTitle+"</font><span class=\"tabright\"><span class=\"hiddencalc inputresult init\"></span></span>");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+"</div>");
							sb.append(enterString);
						}
						sb.append(indentString+indentString+indentString+"</div>");
						sb.append(enterString);
					}
					else
					{
						//bug,没有tab需要塑造一个tab
					}
					
					sb.append(indentString+indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
					sb.append(enterString);
					for(int i=0;i<listFormTab.size();i++)
					{
						Map<String,Object> itemFormTab=listFormTab.get(i);
						
						String formTabId=StringProcess.convertToString(itemFormTab.get("FormTabId"));
						
						sb.append(indentString+indentString+indentString+"<div class=\"fullwidth calcheight onlyscrolly\" id=\"page"+formTabId+"\">");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+"<div class=\"formcontainer calcwidth\">");
						sb.append(enterString);
						
						//FormControl
						for(int j=0;j<listFormControl.size();j++)
						{
							Map<String,Object> itemFormControl=listFormControl.get(j);
							
							String formControlTabId=StringProcess.convertToString(itemFormControl.get("FormTabId"));
							if(!formTabId.equals(formControlTabId))
							{
								continue;
							}
							String formControlId=StringProcess.convertToString(itemFormControl.get("FormControlId"));
							String formControlTitle=StringProcess.convertToString(itemFormControl.get("FormControlTitle")); 
							String widthType=StringProcess.convertToString(itemFormControl.get("WidthType"));
							String widthTypeString="halfwidth";
							if(widthType.equals("2"))
							{
								widthTypeString="fullwidth";
							}
							String heightNumber=StringProcess.convertToString(itemFormControl.get("HeightNumber"));
							String formControlType=StringProcess.convertToString(itemFormControl.get("FormControlType"));
							String preName="e"+formControlType+"_";
							
							sb.append(indentString+indentString+indentString+indentString+indentString+"<div class=\"formitem "+widthTypeString+" h"+heightNumber+"\">");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<div class=\"label w150\">");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString+"<font class=\"labeltext\">"+formControlTitle+"：</font>");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"</div>");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<div class=\"control calcwidth\">");
							sb.append(enterString);
							if(formControlType.equals("11"))
							{
								String minLength=getParams(listFormControlParams,"FormControlId",formControlId,"MinLength");
								String maxLength=getParams(listFormControlParams,"FormControlId",formControlId,"MaxLength");
								String regExp=getParams(listFormControlParams,"FormControlId",formControlId,"RegExp");
								String inheritField=getParams(listFormControlParams,"FormControlId",formControlId,"InheritField");
								String readOnly=getParams(listFormControlParams,"FormControlId",formControlId,"ReadOnly");
								String initValue=getParams(listFormControlParams,"FormControlId",formControlId,"InitValue");
								
								if(inheritField.equals("true"))
								{
									//listTableField
									Map<String,Object> fieldItem=getField(listTableField,listFormSave,formControlId);
									maxLength=StringProcess.convertToString(fieldItem.get("FieldLength"));
									regExp=StringProcess.convertToString(fieldItem.get("FieldRegex"));
								}
								
								String controlName=preName+formControlId;
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<input type=\"text\" class=\"calcwidth\" id=\""+controlName+"\" name=\""+controlName+"\" value=\"\"");
								if(readOnly.length()>0)
								{
									sb.append(" readonly");
								}
								sb.append(" />");
								sb.append(enterString);
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<span class=\"hiddencalc inputresult init\"");
								if(minLength.length()>0)
								{
									sb.append(" minlength=\""+minLength+"\"");
								}
								if(maxLength.length()>0)
								{
									sb.append(" maxlength=\""+maxLength+"\"");
								}
								if(regExp.length()>0)
								{
									sb.append(" validateregular=\""+regExp+"\"");
								}
								sb.append(" controlname=\""+formControlTitle+"\" bindcontrol=\""+controlName+"\"></span>");
								//控件外增加脚本
								if(initValue.length()>0)
								{
									sb.append("<script>$(function() { InitInsertValue('"+controlName+"','"+initValue+"'); });</script>");
								}
								sb.append(enterString);
							}
							else if(formControlType.equals("12"))
							{
								String controlName=preName+formControlId;
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<input type=\"text\" class=\"calcwidth\" id=\""+controlName+"\" name=\""+controlName+"\" value=\"\" onclick=\"c.showMoreDay = true;c.show(this);\" readonly />");
								sb.append("<font><a href=\"javascript:void(0);\" onclick=\"ClearValue('"+controlName+"');\">【清除】</a></font>");
								sb.append(enterString);
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<span class=\"hiddencalc inputresult init\" minlength=\"1\" maxlength=\"50\" controlname=\""+formControlTitle+"\" bindcontrol=\""+controlName+"\"></span>");
								sb.append(enterString);
							}
							else if(formControlType.equals("13"))
							{
								String controlName=preName+formControlId;
								
								String listBoxJson=getParams(listFormControlParams,"FormControlId",formControlId,"JsonValue");
								List<Map<String,Object>> listControlBind=new ArrayList<Map<String, Object>>(); 
								if(listBoxJson.length()>0)
								{
									listControlBind=JSON.parseObject(listBoxJson, new TypeReference<List<Map<String,Object>>>(){});
								}
								
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<select class=\"calcwidth\" id=\""+controlName+"\" name=\""+controlName+"\"/>");
								sb.append(enterString);
								
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("<option value=\"\">请选择</option>");
								sb.append(enterString);
								for(int k=0;k<listControlBind.size();k++)
								{
									String text=StringProcess.convertToString(listControlBind.get(k).get("Text"));
									String value=StringProcess.convertToString(listControlBind.get(k).get("Value"));
									sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
									sb.append("<option value=\""+value+"\">"+text+"</option>");
									sb.append(enterString);
								}
								sb.append(indentString+indentString+indentString+indentString+indentString+indentString+indentString);
								sb.append("</select>");
								sb.append("<span class=\"hiddencalc inputresult init\" minlength=\"1\" maxlength=\"50\" controlname=\""+formControlTitle+"\" bindcontrol=\""+controlName+"\"></span>");
								sb.append(enterString);
							}
							else if(formControlType.equals("14"))
							{
								
							}
							else if(formControlType.equals("15"))
							{
								
							}
							else if(formControlType.equals("16"))
							{
								
							}
							else if(formControlType.equals("17"))
							{
								
							}
							else if(formControlType.equals("18"))
							{
								
							}
							else if(formControlType.equals("19"))
							{
								
							}
							else if(formControlType.equals("20"))
							{
								
							}
							sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"</div>");
							sb.append(enterString);
							sb.append(indentString+indentString+indentString+indentString+indentString+"</div>");
							sb.append(enterString);
						}
						sb.append(indentString+indentString+indentString+indentString+"</div>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+"<div class=\"scrollwidth hiddencalc\" style=\"display: none;\">");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+"<!--滚动条占位宽度-->");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+"</div>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+"</div>");
						sb.append(enterString);
					}
				}
				else if(pageClassifyCode.equals("lby"))
				{
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"<div class=\"formcontainer fullwidth\">");
					sb.append(enterString);
					//遍历FormFilter
					for(int i=0;i<listFormFilter.size();i++)
					{
						String formFilterId=StringProcess.convertToString(listFormFilter.get(i).get("FormFilterId"));
						String formFilterTitle=StringProcess.convertToString(listFormFilter.get(i).get("FormFilterTitle"));
						String formControlType=StringProcess.convertToString(listFormFilter.get(i).get("FormControlType"));
						String preName="e"+formControlType+"_";
						
						sb.append(indentString+indentString+indentString+indentString+"<div class=\"formitem halfwidth h30\">");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+"<div class=\"label w150\">");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<font class=\"labeltext\">"+formFilterTitle+"：</font>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+"</div>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+"<div class=\"control calcwidth\">");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+indentString);
						if(formControlType.equals("11"))
						{
							String controlName=preName+formFilterId;
							
							sb.append("<input type=\"text\" class=\"calcwidth\" id=\""+controlName+"\" name=\""+controlName+"\" />");
						}
						else if(formControlType.equals("12"))
						{
							
						}
						else if(formControlType.equals("13"))
						{
							
						}
						
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+indentString+"</div>");
						sb.append(enterString);
						sb.append(indentString+indentString+indentString+indentString+"</div>");
						sb.append(enterString);
						
					}
					sb.append(indentString+indentString+indentString+"</div>");
					sb.append(enterString);
					
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"<div id=\"content\" class=\"fullwidth calcheight onlyscrolly\">");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"<table style=\"border-collapse: collapse;\" id=\"GridView1\" class=\"showDataTable fullwidth\" border=\"1\" cellspacing=\"0\">");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+indentString+"<tr class=\"showDataTitle\">");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<th scope=\"col\" style=\"width: 50px;\"><p onclick=\"SelectAll('content');\" style=\"cursor: hand\">全选</p></th>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<th style=\"width: 50px;\">序号</th>");
					sb.append(enterString);
					//遍历列
					for(int i=0;i<listFormColumn.size();i++)
					{
						String formColumnId=StringProcess.convertToString(listFormColumn.get(i).get("FormColumnId"));
						String formColumnTitle=StringProcess.convertToString(listFormColumn.get(i).get("FormColumnTitle"));
						
						String width=getParams(listFormColumnParams,"FormColumnId",formColumnId,"Width");
						String widthUnit=getParams(listFormColumnParams,"FormColumnId",formColumnId,"WidthUnit");
						String widthHtml="";
						if(width.length()>0 && widthUnit.length()>0)
						{
							widthHtml="width:"+width+widthUnit+";";
						}
						String styleHtml="";
						if(widthHtml.length()>0)
						{
							styleHtml=" style=\""+widthHtml+"\"";
						}
						
						sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<th"+styleHtml+">"+formColumnTitle+"</th>");
						sb.append(enterString);
					}
					sb.append(indentString+indentString+indentString+indentString+indentString+"</tr>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+indentString+"<tr class=\"showDataRow\" style=\"display: none;\">");
					sb.append(enterString);
					String keyFieldName="";
					for(int j=0;j<listFormRead.size();j++)
					{
						if(StringProcess.convertToString(listFormRead.get(j).get("ReadType")).equals("1"))
						{
							keyFieldName=StringProcess.convertToString(listFormRead.get(j).get("KeyFieldName"));
							break;
						}
					}
					sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<td align=\"center\"><input type=\"checkbox\" value=\"{"+keyFieldName+"}\" /></td>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<td align=\"center\">{IndexNumber}</td>");
					sb.append(enterString);
					//遍历列数据
					for(int i=0;i<listFormColumn.size();i++)
					{
						String formColumnId=StringProcess.convertToString(listFormColumn.get(i).get("FormColumnId"));
						
						String onClick=getParams(listFormColumnParams,"FormColumnId",formColumnId,"OnClick");
						String align=getParams(listFormColumnParams,"FormColumnId",formColumnId,"Align");
						
						String readFieldName="";
						for(int j=0;j<listFormRead.size();j++)
						{
							if(formColumnId.equals(listFormRead.get(j).get("FormColumnId")))
							{
								readFieldName=StringProcess.convertToString(listFormRead.get(j).get("ReadFieldName"));
							}
						}
						String htmlColumnName="";
						if(readFieldName.length()>0)
						{
							htmlColumnName="{"+readFieldName+"}";
						}
						if(onClick.length()>0)
						{
							htmlColumnName="<a href=\"javascript:void(0);\" onclick=\""+onClick.replace("\"", "\\\"")+"\">"+htmlColumnName+"</a>";
						}
						
						String alignHtml="";
						if(align.length()>0)
						{
							alignHtml="text-align:"+align+";";
						}
						String styleHtml="";
						if(alignHtml.length()>0)
						{
							styleHtml=" style=\""+alignHtml+"\"";
						}
						sb.append(indentString+indentString+indentString+indentString+indentString+indentString+"<td"+styleHtml+">"+htmlColumnName+"</td>");
						sb.append(enterString);
					}
					sb.append(indentString+indentString+indentString+indentString+indentString+"</tr>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+indentString+"</table>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"</div>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"<div class=\"fullwidth h30\"><div id=\"displayPage\"></div></div>");
					sb.append(enterString);
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullwidth h4\"></div>");
					sb.append(enterString);
				}
					
				
				
				
				sb.append(indentString+indentString+indentString+"</div>");
				sb.append(enterString);
				if(pageClassifyCode.equals("lby"))
				{
					sb.append(indentString+indentString+indentString+"<div class=\"hiddencalc fullheight w4\"></div>");
					sb.append(enterString);
				}
				sb.append(indentString+indentString+"</div>");
				sb.append(enterString);
				sb.append(indentString+"</form>");
				sb.append(enterString);
				
				
				sb.append("</body>");
				sb.append(enterString);
				sb.append("</html>");
				
				
				//FileUtil.writeFile2(sb.toString(),formBean.getSaveFileName());
				FileUtil.writeFile2(sb.toString(),formBean.getSaveFolderName(),formBean.getSaveFileName());
				FileUtil.writeClassFile2(sb.toString(),formBean.getSaveFolderName(),formBean.getSaveFileName());

				//发布完成
				Map<String, Object> model = new HashMap<String, Object>();
				model.put("isSync", 1);
				model.put("publishTime", new Date());
				model.put("formId", formId);
				formService.update(model);
				
			}
			else
			{
				msg="缺少FormData数据！";
			}
		}
		else
		{
			msg="通过formId找不到Form数据！";
		}
		
		return msg;
	}

	private String getParams(List<Map<String,Object>> list,String objectName,String objectId,String paramsCode)
	{
		String ret="";
		
		for(int i=0;i<list.size();i++)
		{
			Map<String,Object> item=list.get(i);
			
			boolean eq=false;
			if(objectName.length()>0 && objectId.length()>0)
			{
				if(objectId.equals(item.get(objectName)))
				{
					eq=true;
				}
			}
			else
			{
				eq=true;
			}
			
			if(eq)
			{
				if(paramsCode.equals(item.get("ParamsCode")))
				{
					ret=StringProcess.convertToString(item.get("ParamsValue"));
				}
			}
		}
			
		return ret;
	}
	
	private Map<String,Object> getField(List<Object> listTableField,List<Map<String,Object>> listFormSave,String formControlId)
	{
		Map<String,Object> fieldItem = new HashMap<String, Object>();
		
		String saveTableId="";
		String saveFieldId="";
		for(int i=0;i<listFormSave.size();i++)
		{
			if(formControlId.equals(StringProcess.convertToString(listFormSave.get(i).get("FormControlId"))))
			{
				saveTableId=StringProcess.convertToString(listFormSave.get(i).get("SaveTableId"));
				saveFieldId=StringProcess.convertToString(listFormSave.get(i).get("SaveFieldId"));
			}
		}
		
		boolean returnValue=false;
		for(int i=0;i<listTableField.size();i++)
		{
			List<Map<String,Object>> fieldList=(List<Map<String,Object>>)listTableField.get(i);
			for(int j=0;j<fieldList.size();j++)
			{
				if(saveTableId.equals(fieldList.get(j).get("TableId")))
				{
					if(saveFieldId.equals(fieldList.get(j).get("FieldId")))
					{
						fieldItem=fieldList.get(j);
						returnValue=true;
						break;
					}
				}
				else
				{
					break;
				}
			}
			if(returnValue)
			{
				break;
			}
		}
		
		return fieldItem;
	}
	
}
